package bcxd.firstline.credit.machining;

import java.sql.Connection;

import jt.classic.system.context.ISystemContext;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DateTrimmer;

/**
 * 信用机构
 * 
 * @author mido
 * 
 */
public class ContractCreateMachining implements SimpleBeanMachining {

	public SimpleBean machining(SimpleBean bean, Table table,
			CommonBean4HSQ cbh, Connection conn, int type)
			throws MachiningException {
		// TODO Auto-generated method stub
		if (type == -4) {
			if (ISystemContext.invalidSesstion(cbh.getRequest())) {
				throw new MachiningException("invalid session.");
			}
			// 更新会话用户对象信息
			IUser user = ISystemContext.getSessionUser(cbh.getRequest());

			bean.getResource().put("FC_TIME", DateTrimmer.getYMDHMS());
			bean.getResource().put("FC_USERID", user.id());
			bean.getResource().put("FC_USER", user.iname());
			bean.getResource().put("FC_STATUS","0");
		} else if (type == 4000) {
			// 生成合同后将原贷款申请信息归档
			Connection newConn = null;
			try {
				SystemDataSource ds = SystemDataSource.getInstance();
				newConn = ds.getConnection();

				SimpleBean4Table sbt = new SimpleBean4TableImpl("loanapply");
				sbt.getResource().put("LA_STATUS", "2");
				sbt.getResource().put("PK_ID",
						cbh.getResource().get("FC_APPLYID"));

				new BaseworkUtil().manualUpdate1(sbt, newConn);

				SystemDataSource.commitConnection(newConn);
			} catch (Exception ex) {
				SystemDataSource.rollbackConnection(newConn);
				throw new MachiningException("archive loan-apply error.", ex);
			} finally {
				SystemDataSource.closeConnection(newConn);
			}
		}
		return null;
	}

}
